package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"golang.org/x/crypto/bcrypt"
	"log"
	"net/http"
	"wjp/ginandvue/common"
	"wjp/ginandvue/dto"
	"wjp/ginandvue/model"
	"wjp/ginandvue/response"
	"wjp/ginandvue/util"
)

func Register(c *gin.Context) {
	Db := common.Db
	//获取参数 方式二
	//var requestMap = make(map[string]string)
	//json.NewDecoder(c.Request.Body).Decode(&requestMap)
	//获取参数方式三
	var requestUser= model.User{}
	//json.NewDecoder(c.Request.Body).Decode(&requestUser)
	c.Bind(&requestUser)
	name := requestUser.Name
	telephone :=requestUser.Telephone
	password :=requestUser.Password
	//获取参数 方式一
	//name := c.PostForm("name")
	//telephone :=c.PostForm("telephone")
	//password :=c.PostForm("password")
	//数据验证
	if len(telephone)!=11{
		response.Response(c,http.StatusUnprocessableEntity,422,nil,"手机位数必须是11位")
		return
	}
	if len(password)<6{
		response.Response(c,http.StatusUnprocessableEntity,422,nil,"密码不能少于6位")
		return
	}
	//如果名称没有传 给随机数
	if len(name)==0{
		name = util.RandomString(10)
	}
	//判断手机号是否存在
	if isTelephoneExist(Db,telephone){
		response.Response(c,http.StatusUnprocessableEntity,422,nil,"用户已存在")
		return
	}
	//创建用户
	//加密密码
	hashedPassword,err := bcrypt.GenerateFromPassword([]byte(password),bcrypt.DefaultCost)
	if err !=nil{
		response.Response(c,http.StatusInternalServerError,500,nil,"加密失败")
		return
	}
	newUser :=model.User{
		Name:name,
		Telephone:telephone,
		Password:string(hashedPassword),
	}
	Db.Create(&newUser)
	//发送token
	token,err := common.ReleaseToken(newUser)
	if err != nil{
		c.JSON(http.StatusInternalServerError,gin.H{
			"code":500,
			"msg":"系统异常",
		})
		log.Printf("token generate error :%v",err)
		return
	}
	//c.JSON(200, gin.H{
	//	"message": "创建成功",
	//})
	response.Success(c,gin.H{"token":token},"创建成功")
}

func Login(c *gin.Context) {
	Db := common.Db
	//获取参数
	var requestUser= model.User{}
	c.Bind(&requestUser)
	telephone :=requestUser.Telephone
	password :=requestUser.Password
	//数据验证
	if len(telephone)!=11{
		c.JSON(http.StatusUnprocessableEntity,gin.H{
			"code":422,
			"msg":"手机位数必须是11位",
		})
		return
	}
	if len(password)<6{
		c.JSON(http.StatusUnprocessableEntity,gin.H{
			"code":422,
			"msg":"密码不能少于6位",
		})
		return
	}
	var user model.User
	Db.Where("telephone = ?",telephone).First(&user)
	if user.ID ==0{
		c.JSON(http.StatusUnprocessableEntity,gin.H{
			"code":422,
			"msg":"用户不存在",
		})
		return
	}

	//判断密码正确
	if err := bcrypt.CompareHashAndPassword([]byte(user.Password),[]byte(password));err!=nil{
		c.JSON(http.StatusUnprocessableEntity,gin.H{
			"code":400,
			"msg":"密码错诶",
		})
		return
	}
	//发送token
	token,err := common.ReleaseToken(user)
	if err != nil{
		c.JSON(http.StatusInternalServerError,gin.H{
			"code":500,
			"msg":"系统异常",
		})
		log.Printf("token generate error :%v",err)
		return
	}

	response.Success(c,gin.H{"token":token},"登录成功")

}

func Info(c *gin.Context){
	user,_ := c.Get("user")

	c.JSON(http.StatusOK,gin.H{"code":200,"data":gin.H{"user":dto.ToUserDto(user.(model.User))}})
}

func isTelephoneExist(db *gorm.DB,telephone string) bool{
	var user model.User
	db.Where("telephone = ?",telephone).First(&user)
	if user.ID !=0{
		return true
	}
	return false
}